/**
 * 栈的使用
 */

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Stack;

public class Main{

    static class Node{

        int val;
        Node next;

        public Node(int val){
            this.val = val;
        }
    }

    private Node head = null;

    Node create(){
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);

        node1.next = node2;
        node2.next = node3;

        head = node1;

        return head;
    }

    public void reversePrint(){
        Node tem = create();

        Stack<Integer> stack = new Stack<>();

        while(tem != null){
            stack.push(tem.val);
            tem = tem.next;
        }

        //打印
        while(!stack.empty()){
            System.out.print(stack.pop() + " ");
        }
    }

    public static void main(String[] args) {

        Main main = new Main();

        main.reversePrint();
        String s = "as";
        char[] c = s.toCharArray();
        for(char ch : c){

        }

    }
    public static void main1(String[] args){
        Stack<Integer> stack = new Stack<>();

        //push方法：将元素加入栈中
        stack.push(1);//栈底元素
        stack.push(2);//栈顶元素

        //判断栈是否为空
        System.out.println(stack.empty());//false
        //获取栈中元素个数
        System.out.println(stack.size());//2

        //pop方法：将栈顶元素从栈中出去
        stack.pop();//2出栈
        stack.pop();//1出栈

        //判断是否为空
        System.out.println(stack.empty());//true
        //获取元素个数
        System.out.println(stack.size());//0

    }
}

class Solution {
    public static int evalRPN(String[] tokens) {

        //1.判断是否tokens长度是1
        if(tokens.length == 1){
            if(tokens[0].equals("+") ||tokens[0].equals("-")
                    ||tokens[0].equals("*") ||tokens[0].equals("/") )
                return -1;
            else
                return Integer.parseInt(tokens[0]);
        }

        Stack<Integer> stack = new Stack<>();
        //遍历字符串
        for(int i = 0;i < tokens.length;i++){
            if((tokens[i].charAt(0)) >= 48 && (tokens[i].charAt(0)) <= 57 || tokens[i].length() >= 2){
                //入栈
                stack.push(Integer.parseInt(tokens[i]));
            }
            //计算后入栈
            else{
                int num1 = stack.pop();
                int num2 = stack.pop();
                if(tokens[i].equals("+") ){
                    stack.push(num1 + num2);
                }else if(tokens[i].equals("-")){
                    stack.push(num2 - num1);

                }else if(tokens[i].equals("*")){
                    stack.push(num2 * num1);

                }else{
                    stack.push(num2 / num1);
                }
            }
        }
        return stack.pop();
    }

    public static void main(String[] args) {
        String[] tokens = {"10","6","9","3","+","-11","*","/","*","17","+","5","+"};
        int sum = Solution.evalRPN(tokens);
        System.out.println(sum);
        throw new NullPointerException();
    }

}

class MinStack {

    int[] arr;
    int usedSize;
    int min = 0xffffff;

    public MinStack() {
        arr = new int[10];
    }

    public void push(int val) {
        //扩容
        if(usedSize == arr.length){
            arr = Arrays.copyOf(arr,arr.length * 2);
        }
        arr[usedSize++] = val;
        if(min == 0xffffff){
            min = val;
        }else if(min > val){
            min = val;
        }
    }

    public void pop() {
        if(usedSize == 0){
            throw new NullPointerException();
        }else if(usedSize == 1){
            min = 0xffffffff;
        }
        else if(min == arr[usedSize - 1]){
            min = arr[0];
            for(int i = 1;i < usedSize - 1;i++){
                if(min > arr[i]){
                    min = arr[i];
                }
            }
        }
        usedSize--;
    }

    public int top() {
        if(usedSize == 0){
            throw new NullPointerException();

        }else{
            return arr[usedSize -1];
        }
    }

    public int getMin() {
        return min;
    }

    public static void main(String[] args) {
        MinStack min = new MinStack();
        min.push(2147483646);
        min.push(2147483646);
        min.push(2147483647);
        min.top();
        min.pop();
        System.out.println(min.getMin());

        min.pop();
        System.out.println(min.getMin());
        min.pop();
        System.out.println(min.getMin());
        min.pop();
        min.push(2147483647);
        min.top();
        System.out.println(min.getMin());
        min.push(-2147483648);
        min.top();
        System.out.println(min.getMin());
        min.pop();
        System.out.println(min.getMin());

    }
}
//["MinStack","push","push","push","top","pop","getMin","pop",
// "getMin","pop","push","top","getMin","push","top","getMin","pop","getMin"]
  //[[],[2147483646],[2147483646],[2147483647],[],[],[],[],[],[],[2147483647],[],[],[-2147483648],[],[],[],[]]